package com.example.controllers;


import com.example.dao.User_rolesMapper;
import com.example.dao.UsersMapper;
import com.example.pojo.User_roles;
import com.example.pojo.Users;
import com.example.tools.DataBase;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.Map;

@Controller
@RequestMapping
public class TestConltroller {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private User_rolesMapper user_rolesMapper;

    @RequestMapping("/install")
    @ResponseBody
    public String install() throws FileNotFoundException {
        System.out.println(ResourceUtils.getURL("classpath:").getPath());
        DataBase.installDatabase(ResourceUtils.getURL("classpath:").getPath()+"vipmanager.sql", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306", "root", "root");
        return "没报异常";
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test() throws FileNotFoundException {
        Users user = usersMapper.selectByName("admin");
        return user.toString();
    }

    @RequestMapping("/user/ff")
    @ResponseBody
    public String role(){
        return "lll";
    }

    @RequestMapping("/lg")
    public String userHello1(Map<String, Users> map, HttpSession session, String username, String password){
        System.out.println(username+":"+password);
        UsernamePasswordToken upt = new UsernamePasswordToken(username, password);

        // 清除session
        session.removeAttribute("user");

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(upt);
            Users user = usersMapper.selectByName(username);

            // 加入session
            session.setAttribute("user",user);
            System.out.println("登录成功");
            return "helloPage";
        } catch (Exception e) {
            // e.printStackTrace();
            System.out.println("登录失败");
            return "redirect:login.html";            // succss ="bad";
        }

    }

    @RequestMapping("/login")
    //@ResponseBody
    public String login(String username, String password){

        Users user = usersMapper.login(new Users(username,password));

        if(user != null){
            User_roles user_roles = user_rolesMapper.selectRoleName(username);

            if(user_roles.getRole_name().equals("admin")){
                return "是admin";
            }
            if(user_roles.getRole_name().equals("boss")){
                return "boss";
            }
            if(user_roles.getRole_name().equals("user")){
                return "redirect:showuserinfo.html";
            }

        }

        return "失败";
    }

}
